#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(gpyp, 3.8.2, crckyl@gmail.com)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/main.cpp])
AC_CONFIG_HEADER([config.h])
AC_CANONICAL_BUILD

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_RANLIB

AC_SUBST(ac_configure_args)

AC_CHECK_PROG(PROG_WGET,         wget,         yes, no)
AC_CHECK_PROG(PROG_CONVERT,      convert,      yes, no)
AC_CHECK_PROG(PROG_RSVG_CONVERT, rsvg-convert, yes, no)

if test "$PROG_WGET" != "yes"; then
  AC_MSG_ERROR([wget is required])
fi
if test "$PROG_CONVERT" != "yes"; then
  AC_MSG_ERROR([imagemagick is required])
fi
if test "$PROG_RSVG_CONVERT" != "yes"; then
  AC_MSG_ERROR([rsvg-convert is required])
fi

AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [trun on debugging]),
              [if test "$enableval" = "yes"; then
                 CXXFLAGS="-Wall -Wextra -ggdb $CXXFLAGS"
                 CFLAGS="-Wall -Wextra -ggdb $CFLAGS"
                 AC_DEFINE_UNQUOTED(DEBUG, 1, [debug])
               fi])

AC_ARG_ENABLE(gio,
              AS_HELP_STRING([--disable-gio], [disable gio support]),
              [use_gio="$enableval"],
              [use_gio=yes])

AC_ARG_ENABLE(vorbis,
              AS_HELP_STRING([--disable-vorbis], [disable vorbis support(auto detect)]),
              [use_vorbis="$enableval"],
              [use_vorbis="auto"])

AC_ARG_ENABLE(libnotify,
              AS_HELP_STRING([--disable-libnotify], [disable libnotify support]),
              [use_libnotify="$enableval"],
              [use_libnotify=yes])

AC_ARG_ENABLE(growl,
              AS_HELP_STRING([--disable-growl], [disable growl support(darwin)]),
              [use_growl="$enableval"],
              [use_growl=yes])

AC_ARG_WITH([regex],
            AS_HELP_STRING([--with-regex=glib|boost|oniguruma],
                           [specify regex library(default is glib)]),
            [regex_lib="$withval"],
            [regex_lib="glib"])

AC_ARG_ENABLE(status-icon,
              AS_HELP_STRING([--disable-status-icon],
                             [disable status icon]),
              [if test "$enableval" = "no"; then
                 AC_DEFINE_UNQUOTED(DISABLE_STATUS_ICON, , [disable status icon])
               fi])

AC_ARG_ENABLE(dbus,
              AS_HELP_STRING([--disable-dbus], [disable dbus support(auto detect)]),
              [use_dbus="$enableval"],
              [use_dbus="auto"])

AC_ARG_ENABLE(dbus-service,
              AS_HELP_STRING([--enable-dbus-service],
                             [enable dbus service]),
              [enable_dbus_service="$enableval"],
              [enable_dbus_service="no"])

AC_ARG_ENABLE(ss-gnome,
              AS_HELP_STRING([--disable-ss-gnome], [disable gnome-screensaver support]),
              [if test "$enableval" = "no"; then
                 AC_DEFINE_UNQUOTED(DISABLE_SS_GNOME, , [do not support gnome-screensaver])
               fi])

AC_ARG_ENABLE(ss-kde,
              AS_HELP_STRING([--disable-ss-kde], [disable kde-screensaver support]),
              [if test "$enableval" = "no"; then
                 AC_DEFINE_UNQUOTED(DISABLE_SS_KDE, , [do not support kde-screensaver])
               fi])

AC_ARG_ENABLE(ss-xss,
              AS_HELP_STRING([--disable-ss-xss], [disable xscreensaver support]),
              [use_ss_xss="$enableval"
               if test "$enableval" = "no"; then
                 AC_DEFINE_UNQUOTED(DISABLE_SS_XSS, , [do not support xscreensaver])
               fi],
              [use_ss_xss="yes"])

AC_ARG_ENABLE(desktop-files,
              AS_HELP_STRING([--disable-desktop-files],
                             [do not install .desktop files]),
              [install_desktop_files="$enableval"],
              [install_desktop_files="yes"])
AM_CONDITIONAL(INSTALL_DESKTOP_FILES, test "$install_desktop_files" = "yes")

AC_ARG_ENABLE(favicon,
              AS_HELP_STRING([--disable-favicon],
                             [do not install favicons]),
              [install_favicon="$enableval"],
              [install_favicon="yes"])
AM_CONDITIONAL(INSTALL_FAVICON, test "$install_favicon" = "yes")

have_svn=no
if test -d ".svn"; then
  have_svn=yes
fi
AM_CONDITIONAL(SVN, test "$have_svn" = "yes")

case "$build_os" in
  *linux*)   os_name=linux ;;
  *bsd*)     os_name=bsd ;;
  *darwin*)  os_name=darwin ;;
  *solaris*) os_name=solaris ;;
  *)         os_name=unknown ;;
esac
AM_CONDITIONAL(OS_DARWIN, test "$os_name" = "darwin")

# Checks for libraries.
PKG_CHECK_MODULES(X11, x11, , AC_MSG_ERROR([x11 is required]))
AC_SUBST(X11_CFLAGS)
AC_SUBST(X11_LIBS)

PKG_CHECK_MODULES(GTKMM, gtkmm-2.4 >= 2.12.0, ,
                  [if test "$regex_lib" != "glib"; then
                     PKG_CHECK_MODULES(GTKMM, gtkmm-2.4 >= 2.8.0)
                   else
                     AC_MSG_ERROR([gtkmm-2.4 >= 2.12.0(or >= 2.8.0 with --with-regex=boost|oniguruma) is required])
                   fi])
AC_SUBST(GTKMM_CFLAGS)
AC_SUBST(GTKMM_LIBS)

PKG_CHECK_MODULES(GTHREAD, gthread-2.0 >= 2.10.0)
AC_SUBST(GTHREAD_CFLAGS)
AC_SUBST(GTHREAD_LIBS)

if test "$use_gio" = "yes"; then
  PKG_CHECK_MODULES(GIO, gio-unix-2.0 >= 2.16.0)
  AC_DEFINE_UNQUOTED(USE_GIO, , [use gio])
fi
AC_SUBST(GIO_CFLAGS)
AC_SUBST(GIO_LIBS)

PKG_CHECK_MODULES(LIBAO, ao >= 0.8.6)
AC_SUBST(LIBAO_CFLAGS)
AC_SUBST(LIBAO_LIBS)

if test "$use_vorbis" = "auto"; then
  PKG_CHECK_MODULES(VORBIS, vorbis >= 1.2.0 vorbisfile >= 1.2.0,
                    [AC_DEFINE_UNQUOTED(USE_VORBIS, , [use libvorbis])], [:])
fi
if test "$use_vorbis" = "yes"; then
  PKG_CHECK_MODULES(VORBIS, vorbis >= 1.2.0 vorbisfile >= 1.2.0,
                    [AC_DEFINE_UNQUOTED(USE_VORBIS, , [use libvorbis])])
fi
AC_SUBST(VORBIS_CFLAGS)
AC_SUBST(VORBIS_LIBS)

if test "$os_name" = "darwin" -a "$use_growl" = "yes"; then
  AC_DEFINE_UNQUOTED(USE_GROWL, , [use growl])
  AC_GROWL(, [AC_MSG_ERROR([Growl is required])])
elif test "$use_libnotify" = "yes"; then
  AC_DEFINE_UNQUOTED(USE_LIBNOTIFY, , [use libnotify])
  PKG_CHECK_MODULES(LIBNOTIFY, libnotify >= 0.4.2);
  AC_MSG_CHECKING([for notification closed signal has reason argument])
  AC_LANG_PUSH(C)
  CFLAGS_SAVE="$CFLAGS"
  CFLAGS="$CFLAGS $LIBNOTIFY_CFLAGS"
  AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[@%:@include <libnotify/notify.h>]],
                                    [[NotifyNotificationClass n;n.closed(NULL, 1);]]),
                    [AC_MSG_RESULT(yes)
                     AC_DEFINE_UNQUOTED(NOTIFY_CLOSED_HAS_REASON, ,
                                        [NotifyNotification closed signal has reason argument])],
                    [AC_MSG_RESULT(no)])
  CFLAGS="$CFLAGS_SAVE"
  AC_LANG_POP
fi
AC_SUBST(LIBNOTIFY_CFLAGS)
AC_SUBST(LIBNOTIFY_LIBS)

AC_MIGEMO([1.3], [AC_DEFINE_UNQUOTED(USE_MIGEMO, , [use migemo])])

if test "$use_dbus" = "auto"; then
  PKG_CHECK_MODULES(DBUS_GLIB, dbus-glib-1 >= 0.60,
                    [AC_DEFINE_UNQUOTED(USE_DBUS_GLIB, , [use dbus-glib])], [:])
fi
if test "$use_dbus" = "yes"; then
  PKG_CHECK_MODULES(DBUS_GLIB, dbus-glib-1 >= 0.60,
                    [AC_DEFINE_UNQUOTED(USE_DBUS_GLIB, , [use dbus-glib])])
fi
AM_CONDITIONAL(ENABLE_DBUS_SERVICE, test "$enable_dbus_service" = "yes")
if test "$enable_dbus_service" = "yes"; then
  AC_DEFINE_UNQUOTED(ENABLE_DBUS_SERVICE, , [enable dbus-service])
fi
AC_SUBST(DBUS_GLIB_CFLAGS)
AC_SUBST(DBUS_GLIB_LIBS)

if test "$use_ss_xss" = "yes"; then
  AC_PATH_XTRA
fi
AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(X_PRE_LIBS)

AX_BOOST(1.34)
AX_BOOST_ASIO()

AC_ARG_VAR([ASIO_CPPFLAGS], [C compiler flags for asio])
AC_ARG_VAR([ASIO_LIBS], [linker flags for asio])

AC_LANG_PUSH(C++)
CPPFLAGS_SAVE="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $ASIO_CPPFLAGS $BOOST_CPPFLAGS"
LDFLAGS_SAVE="$LDFLAGS"
LDFLAGS="$LDFLAGS $ASIO_LIBS"
AC_CHECK_HEADERS([asio.hpp], [ax_cv_asio=yes], [ax_cv_asio=no])
CPPFLAGS="$CPPFLAGS_SAVE"
LDFLAGS="$LDFLAGS_SAVE"
AC_LANG_POP

if test "$ax_cv_boost_date_time" != "yes"; then
  AC_MSG_ERROR([boost-date-time is required])
fi
if test "$ax_cv_boost_date_time_lib" != "yes"; then
  AC_MSG_ERROR([boost-date-time library is required])
fi
if test "$ax_cv_boost_regex" != "yes"; then
  AC_MSG_ERROR([boost-regex is required])
fi
if test "$ax_cv_boost_serialization" != "yes"; then
  AC_MSG_ERROR([boost-serialization is required])
fi
if test "$ax_cv_boost_asio" = "yes"; then
  if test "$ax_cv_boost_asio_lib" != "yes"; then
    AC_MSG_ERROR([boost-system library is required])
  fi
else
  if test "$ax_cv_asio" != "yes"; then
    AC_MSG_ERROR([boost-asio or asio.hpp is required])
  fi
fi
AC_SUBST(ASIO_CPPFLAGS)
AC_SUBST(ASIO_LIBS)

if test "$regex_lib" = "boost"; then
  if test "$ax_cv_boost_regex_lib" != "yes"; then
    AC_MSG_ERROR([boost-regex library is required])
  fi

  AC_LANG_PUSH(C++)
  CPPFLAGS_SAVE="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
  AC_CHECK_HEADERS([boost/regex/icu.hpp],\
                   [AC_DEFINE_UNQUOTED(USE_BOOST_REGEX, , [use boost::regex])],
                   [AC_MSG_ERROR([boost regex icu support required])])
  CPPFLAGS="$CPPFLAGS_SAVE"
  AC_LANG_POP
fi
if test "$regex_lib" = "oniguruma"; then
  AC_ONIGURUMA([5.9.1])
  AC_DEFINE_UNQUOTED(USE_ONIGURUMA, , [use oniguruma])
fi

# Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_HEADER_STDBOOL
AC_CHECK_HEADERS([stdlib.h string.h langinfo.h fcntl.h stdint.h sys/types.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T

AC_STRUCT_TM

AC_CHECK_SIZEOF(unsigned int)

mod=`expr 16 % $ac_cv_sizeof_unsigned_int = 0`
if test "$mod" = "0"; then
  AC_MSG_ERROR([can not divide 16 by size of unsigned int])
fi

# Checks for library functions.
AC_FUNC_ERROR_AT_LINE
AC_FUNC_FORK
AC_FUNC_SETVBUF_REVERSED
AC_FUNC_STRFTIME
AC_CHECK_FUNCS([dup2 memset pow strchr strrchr strstr strtol uname localtime_r setenv setlocale strcasecmp fdatasync sqrt strncasecmp])

AC_MSG_CHECKING([for type of iconv 2nd argument])
AC_LANG_PUSH(C++)
AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[@%:@include <iconv.h>]],
                                  [[iconv((iconv_t)0, (char **)0, 0, 0, 0);]]),
                  [iconv_type_arg2="char **"],
                  [iconv_type_arg2="const char **"])
AC_MSG_RESULT([$iconv_type_arg2])
AC_DEFINE_UNQUOTED([ICONV_TYPE_ARG2], [($iconv_type_arg2)],
                   [type of iconv 2nd argument])
AC_LANG_POP

IT_PROG_INTLTOOL([0.35.0])

GETTEXT_PACKAGE=$PACKAGE
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"], [The domain to use with gettext])
ALL_LINGUAS="ja"
AM_GLIB_GNU_GETTEXT

pixmapsdir=$datadir/pixmaps
pkgpixmapsdir=$pixmapsdir/$PACKAGE
pkgpixmaps_16dir=$pkgpixmapsdir/16
pkgpixmaps_22dir=$pkgpixmapsdir/22
pkgpixmaps_scalabledir=$pkgpixmapsdir/scalable
AC_SUBST(pixmapsdir)
AC_SUBST(pkgpixmapsdir)
AC_SUBST(pkgpixmaps_16dir)
AC_SUBST(pkgpixmaps_22dir)
AC_SUBST(pkgpixmaps_scalabledir)

AC_CONFIG_FILES([Makefile
                 po/Makefile.in
                 src/Makefile
                 src/conf/Makefile
                 src/pref/Makefile
                 src/icons/Makefile
                 src/icons/icons/Makefile
                 src/icons/icons/16/Makefile
                 src/icons/icons/22/Makefile
                 src/icons/icons/scalable/Makefile
                 src/icons/favicon/Makefile
                 src/normal_re/Makefile
                 src/test/Makefile])
AC_OUTPUT
